package com.project.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.project.bean.StudentBean;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;

public class TestServlet extends HttpServlet {
    public TestServlet(){
        System.out.println("servlet构造");
    }
    @Override
    public void init() throws ServletException {
        System.out.println("init初始化方法");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置请求信息编码集，对post有效
        req.setCharacterEncoding("utf-8");
        //设置响应信息的MIME类型和编码集
        resp.setContentType("text/html;charset=utf-8");
        //得到客户端的表单数据
        String name = req.getParameter("userName");
//        String hibby = req.getParameter("hibby");
        //得到多个同名的表单数据
        String[] hibby = req.getParameterValues("hibby");

        resp.getWriter().print(name+"   "+ Arrays.toString(hibby));

//        StudentBean student = new StudentBean(
//                "高义","男","本科", LocalDate.parse("1998-03-04"));
//        List<StudentBean> list = List.of(
//                new StudentBean(
//                        "高义","男","本科", LocalDate.parse("1998-03-04")),
//                new StudentBean(
//                "高进","男","大专", LocalDate.parse("1965-03-04")),
//                new StudentBean(
//                "高老庄","男","本科", LocalDate.parse("1976-03-04"))
//        );


//        ObjectMapper om = new ObjectMapper();
//        //设置日期支持
//        om.registerModule(new JavaTimeModule());
//        //将对象转化为json字符串
//        String str = om.writeValueAsString(list);
//        resp.getWriter().print(str);
    }
}
